<?php
/**
 * 后台用户组服务类
 *
 * @author liang<lang@vip.deyi.com>
 * @since   2016/11/30 10:11
 * @copyright  deyi.com
 */
namespace Apps\Acp\Services;

use Apps\Acp\Models\ManagerGroup;
use Apps\Common\Constant\CacheKeyConstant;
use Apps\Common\Constant\CacheTimeoutConstant;
use Deyi\Functions\CacheFunction;

/**
 * Class ManagerGroupService
 * @package Apps\Acp\Services
 */
class ManagerGroupService extends ManagerGroup
{
    public function getPrivilegesByGid($gid)
    {
        $cacheKey = CacheFunction::generateKey(CacheKeyConstant::ACP_MANAGER_GROUP_CACHE_KEY, $gid);

        $res = static::$_memcached->get($cacheKey);

        if (is_array($res)) {
            return $res;
        }

        $res = ManagerGroup::getInstance()->getOneByField('id', $gid);

        if (!$res) {
            $res = [];
        } else {
            $res = explode(',', $res->privilege);
        }

        static::$_memcached->set($cacheKey, $res, CacheTimeoutConstant::LOCAL_MAX_CACHE);

        return $res;
    }

    public function cleanPrivilegesByGid($gid)
    {
        $cacheKey = CacheFunction::generateKey(CacheKeyConstant::ACP_MANAGER_GROUP_CACHE_KEY, $gid);

        static::$_memcached->delete($cacheKey);
    }
}